package com.github.caiqy.shirodemo.core;

import java.util.*;

/**
 * 放一些常用的工具
 */
public class CommonUtils {
    private static final String SEPARATOR = ",";

    public static String list2Str(Iterable iterable) {
        return list2Str(iterable, SEPARATOR);
    }

    public static String list2Str(Iterable<?> iterable, String separator) {
        return join(iterable, separator);
    }

    public static List<Integer> str2IntList(String str) {
        return str2IntList(str, SEPARATOR);
    }

    public static List<Integer> str2IntList(String str, String separator) {
        if (str == null || "".equals(str))
            return null;
        List<Integer> result = new ArrayList<>();
        for (String s : str.split(separator)) {
            result.add(Integer.parseInt(s));
        }
        return result;
    }

    public static String join(Iterable<?> iterable, String separator) {
        return iterable == null ? null : join(iterable.iterator(), separator);
    }

    public static String join(Iterator<?> iterator, String separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                return Objects.toString(first, "");
            } else {
                StringBuilder buf = new StringBuilder(256);
                if (first != null) {
                    buf.append(first);
                }

                while (iterator.hasNext()) {
                    if (separator != null) {
                        buf.append(separator);
                    }

                    Object obj = iterator.next();
                    if (obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String join(Iterable<?> iterable, char separator) {
        return iterable == null ? null : join(iterable.iterator(), separator);
    }

    public static String join(Iterator<?> iterator, char separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                return Objects.toString(first, "");
            } else {
                StringBuilder buf = new StringBuilder(256);
                if (first != null) {
                    buf.append(first);
                }

                while (iterator.hasNext()) {
                    buf.append(separator);
                    Object obj = iterator.next();
                    if (obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static boolean isEmpty(String s) {
        return s == null || s.length() == 0;
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }
}
